package com.liaogui.oss.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

import java.net.URI;

/**
 * @author liaogui
 * @date 2025/1/21 20:55
 */
@Configuration
public class S3ClientConfig {

    @Value("${s3.uri}")
    private String uri;

    @Value("${s3.accessKeyId}")
    private String accessKeyId;

    @Value("${s3.secretAccessKey}")
    private String secretAccessKey;

    @Bean
    public S3Client s3Client(){
        return S3Client.builder()
                // 替换为你的MinIO服务器URL
                .endpointOverride(URI.create(uri))
                // MinIO不使用区域，但AWS SDK需要一个区域 指定北京
                .region(Region.CN_NORTH_1)
                .credentialsProvider(StaticCredentialsProvider.create(
                        // 替换为你的访问密钥和密钥
                        AwsBasicCredentials.create(accessKeyId, secretAccessKey)
                ))
                .build();
    }
}
